package main.java.cn.wolfcode.service.impl;

import cn.wolfcode.domain.Role;
import cn.wolfcode.mapper.RoleMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServieImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void insert(Role role,Long[] ids) {
        roleMapper.insert(role);
        if (ids!=null&&ids.length>0){
                //处理中间表
               roleMapper.insertRelation(role.getId(),ids);
            }
    }
    @Override
    public void deleteByPrimaryKey(Long id) {
        roleMapper.deleteByPrimaryKey(id);
        roleMapper.deleteRelation(id);
    }

    @Override
    public void updateByPrimaryKey(Role role,Long[] ids) {
        //更新角色信息
        roleMapper.updateByPrimaryKey(role);
        //删除中间表的关系
        roleMapper.deleteRelation(role.getId());
        //重新添加即为更新
        if (ids!=null&&ids.length>0){
            roleMapper.insertRelation(role.getId(),ids);
        }
    }

    @Override
    public Role selectByPrimaryKey(Long id) {
        Role role = roleMapper.selectByPrimaryKey(id);
        return role;
    }

    @Override
    public List<Role> selectAll() {
        List<Role> list = roleMapper.selectAll();
        return list;
    }

    @Override
    public PageInfo<Role> queryForList(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Role> roles = roleMapper.queryForList(qo);
        return new PageInfo<Role>(roles);
    }
}
